package com.ebt.m.utils.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.ebt.m.AppContext;
import com.ebt.m.data.bean.CardInfo;
import com.ebt.m.data.bean.UserInfo;
import com.ebt.m.utils.ConfigData;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/***
 * 获取用户账户密码，以及公钥
 *
 * @author andy.wang
 */
public class EBTUserInfoHelper {

    public static void updatePrivateKey(String privateKey) {
        SharedPreferences sharedPreferences = AppContext.getInstance().getSharedPreferences(
                ConfigData.SHAREDPREFERENCES_USERKEYINFO, 0);
        Editor edit = sharedPreferences.edit();
        edit.putString(ConfigData.SHAREDPREFERENCES_PRIVATE_KEY, privateKey);
        edit.commit();
    }

    public static UserInfo getUserFromPreferences() {
        Context context = AppContext.getInstance();
        UserInfo user = new UserInfo();
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                ConfigData.SHAREDPREFERENCES_USERKEYINFO, 0);

        user.setUserName(
                sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_USERNAME, ""));
        user.setPassword(
                sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_PASSWORD, ""));
        user.setUserPublicKey(
                sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_PUBLICK_KEY, ""));
        user.setUserPrivateKey(
                sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_PRIVATE_KEY, ""));
        user.setUserId(
                Integer.valueOf(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_USERID, "0")));
        user.setAccessToken(
                sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_ACCESSTOKEN, ""));
        user.setValidateStatus(
                sharedPreferences.getInt(ConfigData.SHAREDPREFERENCES_AUTHENTICATIONSTATUS, 1));
        user.setPhone(
                sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_PHONE, ""));

        int companyId = sharedPreferences.getInt(ConfigData.SHAREDPREFERENCES_COMPANYID, 0);
        if (companyId > 0) {
            UserInfo.CompanyInfo companyInfo = new UserInfo.CompanyInfo();
            companyInfo.setCompanyID(companyId);
            companyInfo.setLogoUrl(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_COMPANY_IMG, ""));
            companyInfo.setMiniLogoUrl(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_COMPANY_MINI_IMG, ""));
            companyInfo.setCompanyName(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_COMPANY_NAME, ""));
            companyInfo.setCategory(sharedPreferences.getInt(ConfigData.SHAREDPREFERENCES_COMPANYCATEGORY, 1));
            companyInfo.setNameForZyj(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_COMPANY_NAME_FOR_ZYJ, ""));
            user.setCompanyInfo(companyInfo);
        }

        String employeeNo = sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_EMPLOYEENO, "");
        if (!TextUtils.isEmpty(employeeNo)) {
            CardInfo cardInfo = new CardInfo();
            cardInfo.setEmployeeNo(employeeNo);
            cardInfo.setCompanyName(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_COMPANYNAME, ""));
            cardInfo.setImgUrl(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_IMG_URL, ""));
            cardInfo.setName(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_NAME, ""));
            cardInfo.setPhone(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_ERP_PHONE, ""));
            cardInfo.setNickName(sharedPreferences.getString(ConfigData.SHAREDPREFERENCES_NICK_NAME, ""));
            user.setCardInfo(cardInfo);
        }
        if (user.getUserId() == 0 || TextUtils.isEmpty(user.getAccessToken())) {
            user.clearUserInfo();
            return user;
        }
        return user;
    }

    /**
     * 保存用户信息，公钥信息
     *
     * @param user
     * @return
     */
    public static UserInfo saveUserKEY(UserInfo user) {
        Context context = AppContext.getInstance();
        SharedPreferences sharedPreferences = context.getSharedPreferences(
                ConfigData.SHAREDPREFERENCES_USERKEYINFO,
                0);
        Editor edit = sharedPreferences.edit();
        edit.putString(ConfigData.SHAREDPREFERENCES_USERNAME, user.getUserName());
        edit.putString(ConfigData.SHAREDPREFERENCES_PASSWORD, user.getPassword());
        edit.putString(ConfigData.SHAREDPREFERENCES_PRIVATE_KEY, user.getUserPrivateKey());
        edit.putString(ConfigData.SHAREDPREFERENCES_PUBLICK_KEY, user.getUserPublicKey());
        edit.putString(ConfigData.SHAREDPREFERENCES_USERID, user.getUserId() + "");
        edit.putString(ConfigData.SHAREDPREFERENCES_ACCESSTOKEN, user.getAccessToken());
        edit.putInt(ConfigData.SHAREDPREFERENCES_AUTHENTICATIONSTATUS, user.getValidateStatus());
        edit.putString(ConfigData.SHAREDPREFERENCES_PHONE, user.getPhone());
        if (user.getCompanyInfo() != null) {
            edit.putInt(ConfigData.SHAREDPREFERENCES_COMPANYID, user.getCompanyInfo().getCompanyID());
            edit.putString(ConfigData.SHAREDPREFERENCES_COMPANY_NAME, user.getCompanyInfo().getCompanyName());
            edit.putString(ConfigData.SHAREDPREFERENCES_COMPANY_IMG, user.getCompanyInfo().getLogoUrl());
            edit.putString(ConfigData.SHAREDPREFERENCES_COMPANY_MINI_IMG, user.getCompanyInfo().getMiniLogoUrl());
            edit.putInt(ConfigData.SHAREDPREFERENCES_COMPANYCATEGORY, user.getCompanyInfo().getCategory());
            edit.putString(ConfigData.SHAREDPREFERENCES_COMPANY_NAME_FOR_ZYJ, user.getCompanyInfo().getNameForZyj());
        }

        if (user.getCardInfo() != null) {
            edit.putString(ConfigData.SHAREDPREFERENCES_ERP_PHONE, user.getCardInfo().getPhone());
            edit.putString(ConfigData.SHAREDPREFERENCES_COMPANYNAME, user.getCardInfo().getCompanyName());
            edit.putString(ConfigData.SHAREDPREFERENCES_EMPLOYEENO, user.getCardInfo().getEmployeeNo());
            edit.putString(ConfigData.SHAREDPREFERENCES_IMG_URL, user.getCardInfo().getImgUrl());
            edit.putString(ConfigData.SHAREDPREFERENCES_NAME, user.getCardInfo().getName());
            edit.putString(ConfigData.SHAREDPREFERENCES_NICK_NAME, user.getCardInfo().getNickName());
        }
        edit.commit();
        return user;
    }

    /***
     * 解析服务器上的公钥json，获取公钥
     *
     * @param keyJson
     * @return
     */
    public static String getUserkeyValue(String keyJson) {
        String public_key = null;
        if (keyJson != null && !keyJson.isEmpty()) {
            JSONTokener jsonParser = new JSONTokener(keyJson);
            try {
                JSONObject obj = (JSONObject) jsonParser.nextValue();
                String flag = obj.getString("validate");
                if (flag != null && "1".equals(flag)) {
                    public_key = obj.getString("key");
                    return public_key;
                } else {
                    return null;
                }
            } catch (JSONException e1) {
                return null;
            } catch (Exception e1) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static void deleteUserInfo() {
        Context context = AppContext.getInstance();

        SharedPreferences sharedPreferences = context.getSharedPreferences(
                ConfigData.SHAREDPREFERENCES_USERKEYINFO, 0);

        Editor edit = sharedPreferences.edit();
        edit.remove(ConfigData.SHAREDPREFERENCES_USERID);
        edit.remove(ConfigData.SHAREDPREFERENCES_USERNAME);
        edit.remove(ConfigData.SHAREDPREFERENCES_PASSWORD);
        edit.remove(ConfigData.SHAREDPREFERENCES_PRIVATE_KEY);
        edit.remove(ConfigData.SHAREDPREFERENCES_PUBLICK_KEY);
        edit.remove(ConfigData.SHAREDPREFERENCES_ACCESSTOKEN);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANY_IMG);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANY_MINI_IMG);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANYID);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANY_NAME);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANYCATEGORY);
        edit.remove(ConfigData.SHAREDPREFERENCES_AUTHENTICATIONSTATUS);
        edit.remove(ConfigData.SHAREDPREFERENCES_PHONE);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANY_NAME_FOR_ZYJ);
        edit.remove(ConfigData.SHAREDPREFERENCES_ERP_PHONE);
        edit.remove(ConfigData.SHAREDPREFERENCES_COMPANYNAME);
        edit.remove(ConfigData.SHAREDPREFERENCES_EMPLOYEENO);
        edit.remove(ConfigData.SHAREDPREFERENCES_IMG_URL);
        edit.remove(ConfigData.SHAREDPREFERENCES_NAME);
        edit.remove(ConfigData.SHAREDPREFERENCES_NICK_NAME);
        edit.commit();
    }
}